<?php 
class search
{
	var $db;
	var $table; 
	var $segment;
	var $datadir;
	var $total;
	var $titlelen;
	var $descriptionlen;
	var $ft_min_word_len;
	var $color;

    function __construct()
    {
		global $db,$MODULE;
		$this->db = &$db;
		$this->table = DB_PRE.'content';
		$this->datadir = WWW_ROOT.'sites/search/';
		$this->ft_min_word_len = $this->ft_min_word_len();
		$this->set();
    }

	function search()
	{
		$this->__construct();
	}

	function set($titlelen = 80, $descriptionlen = 500, $color = 'red')
	{
		$this->titlelen = max(intval($titlelen), 100);
		$this->descriptionlen = max(intval($descriptionlen), 50);
		$this->color = $color;
	} 

	function add($title, $content, $url)
	{
		$data = $this->segment($title.$content);
		$data = $title.' '.$data;
		$data = $this->db->escape($data); 
		$searchid = $this->db->insert_id();
        $this->set_data($searchid, array('title'=>$title, 'content'=>$content, 'url'=>$url));
		return $searchid;
	}

	function update($searchid, $title, $content, $url)
	{
		if($this->db->get_one("SELECT `searchid` FROM `$this->table` WHERE `searchid`='$searchid'"))
		{
			$data = $this->segment($title.$content);
			$data = $title.' '.$data;
			$data = $this->db->escape($data); 
			$this->set_data($searchid, array('title'=>$title, 'content'=>$content, 'url'=>$url));
		}
		else
		{
			$this->add($title, $content, $url);
		}
		return true;
	}

	function delete($id, $field = 'searchid')
	{
		$id = intval($id);
        if($field == 'searchid')
		{
			$this->db->query("DELETE FROM `$this->table` WHERE `searchid`=$id");
			if($this->db->affected_rows() == 0) return false;
			$this->set_data($id);
		} 
		return true;
	}
	
	function q($q, $order = 0, $page = 1, $pasesize = 10, $is_red = 1)
	{
		global $M;
		$page = max(intval($page), 1);
		$offset = $pasesize*($page-1); 
		
		$where = " `title` LIKE '%$q%' and status = 99 "; 
		
		$this->total = cache_count("SELECT COUNT(*) AS `count` FROM `$this->table` WHERE $where");
		if($this->total == 0) return array();
		
		$this->pages = pages($this->total, $page, $pasesize);
		$orderby = $order ? " ORDER BY `searchid` DESC " : '';
		
		$array = array();
		$result = $this->db->query("SELECT * FROM `$this->table` WHERE $where $orderby LIMIT $offset, $pasesize");
		while($r = $this->db->fetch_array($result))
		{
			$data['date'] = date('Y-m-d', $r['time']);
			$data['title'] = strip_tags($r['title']);
			$data['url'] = $r['url'];
			$data['content'] = strip_tags($r['content']);
			if($is_red)
			{
				$data['title'] = $this->red($data['title'], $q, $this->color, $this->titlelen);
				$data['content'] = $this->red($data['content'], $q, $this->color, $this->descriptionlen);
			}
			$array[] = $data;
		}
        $this->db->free_result($result);
		return $array;
	} 
	
	function strip($q)
	{
		return str_replace(array('\\', '"', "'"), '', trim($q));
	}

	function red($string, $words, $color = 'red', $strlen = 0)
	{
		if($string == '' || $words == '') return '';
		$position = $search = $replace = array();
		if(!is_array($words)) $words = explode(' ', $words);
		foreach($words as $k=>$word)
		{
			if(!$word || strpos($string, $word) === false) continue;
			$search[$k] = $word;
			$replace[$k] = '<font color="'.$color.'">'.$word.'</font>';
			if($k == 2) break;
		}
		if($strlen) $string = str_cut($string, $strlen);
		return str_replace($search, $replace, $string);
	}

	function segment($content)
	{
		global $SETTING;
		if(!is_object($this->segment)) $this->segment = load('segment.class.php','segment');
		$this->segment->set_text($content);
		return $this->segment->get_words();
	}  

	function create_index()
	{
		@set_time_limit(600);
		$this->db->query("ALTER TABLE `$this->table` DROP INDEX `data`");
		return $this->db->query("ALTER TABLE `$this->table` ADD FULLTEXT (`data`)");
	}

	function ft_min_word_len()
	{
		$r = $this->db->get_one("SHOW VARIABLES LIKE 'ft_min_word_len'");
		return $r ? $r['Value'] : 4;
	}
}
?>